package com.chlitina.store.modules.cash.model;

import java.util.List;

import com.chlitina.store.modules.cash.model.recharge.Recharge;
import com.chlitina.store.modules.cash.model.recharge.RechargeElementManager;
import com.chlitina.store.modules.cash.model.recharge.RehcargeHost;

public class RechargePayment {
	private float totalTimes;
	private float totalAll;
	private float totalProduct;
	private float totalDuration;

	private float totalCost;
	private float cashPay;
	private float cardPay;

	public float getTotalTimes() {
		return totalTimes;
	}

	public void setTotalTimes(float totalTimes) {
		this.totalTimes = totalTimes;
	}

	public float getTotalAll() {
		return totalAll;
	}

	public void setTotalAll(float totalAll) {
		this.totalAll = totalAll;
	}

	public float getTotalProduct() {
		return totalProduct;
	}

	public void setTotalProduct(float totalProduct) {
		this.totalProduct = totalProduct;
	}

	public float getTotalDuration() {
		return totalDuration;
	}

	public void setTotalDuration(float totalDuration) {
		this.totalDuration = totalDuration;
	}

	/**
	 * 0: total 1: byCash 2: byCard
	 * 
	 * @param hosts
	 * @return
	 */
	private float[] calculateTotal(List<RehcargeHost> hosts) {
		float[] result = new float[3];
		if (hosts != null) {
			for (RehcargeHost host : hosts) {
				List<Recharge> recharges = host.getRecharges();
				for (Recharge recharge : recharges) {
					result[0] += recharge.getMoney();
					AllCardPaymentDetail cardAll = recharge.getPayment()
							.getCardAll();
					if (cardAll == null) {
						result[1] += recharge.getPayment().getCash();
					} else {
						result[2] += cardAll.getRealCost();
					}
				}
			}
		}
		return result;
	}

	private void reset() {
		totalTimes = 0;
		totalDuration = 0;
		totalProduct = 0;
		totalAll = 0;
		totalCost = 0;
		cardPay = 0;
		cashPay = 0;
	}
	//0: total 1: byCash 2: byCard
	private void updateCashCardPay(	float[] temp){
		if (temp != null) {
			cashPay+=temp[1];
			cardPay+=temp[2];
		}
	}
	
	public void update(RechargeElementManager manger) {
		reset();

		//times
		//0: total 1: byCash 2: byCard
		float[] temp = calculateTotal(manger.getRechargedCardTimes());
		if (temp != null) {
			totalTimes=temp[0];
			updateCashCardPay(temp);
		}

		//duration
		temp = calculateTotal(manger.getRechargedCardDuration());
		if (temp != null) {
			totalDuration=temp[0];
			updateCashCardPay(temp);
		}
		//product
		temp = calculateTotal(manger.getRechargedCardProduct());
		if (temp != null) {
			totalProduct=temp[0];
			updateCashCardPay(temp);
		}
		//all
		temp = calculateTotal(manger.getRechargedCardAll());
		if (temp != null) {
			totalAll=temp[0];
			updateCashCardPay(temp);
		}
		totalCost = totalTimes + totalDuration + totalProduct + totalAll;
	}

	public float getTotalCost() {
		return totalCost;
	}

	public void setTotalCost(float totalCost) {
		this.totalCost = totalCost;
	}

	public float getCashPay() {
		return cashPay;
	}

	public void setCashPay(float cashPay) {
		this.cashPay = cashPay;
	}

	public float getCardPay() {
		return cardPay;
	}

	public void setCardPay(float cardPay) {
		this.cardPay = cardPay;
	}

}
